package com.sdkj.recruit.service.impl;

import java.io.File;
import java.io.InputStream;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.sdkj.recruit.model.ResultCode;
import com.sdkj.recruit.model.ResultData;
import com.sdkj.recruit.service.IQiNiuService;
import com.sdkj.recruit.util.PropertiesUtil;

@Service
public class QiNiuService implements IQiNiuService {

	private static final Logger logger = Logger.getLogger(QiNiuService.class);
	private static PropertiesUtil propertiesUtil = new PropertiesUtil("config.properties");

	@Override
	public ResultData<Object> uploadToken() {

		ResultData<Object> result = new ResultData<>();
		String accessKey = propertiesUtil.getProperty("AccessKey");
		String secretKey = propertiesUtil.getProperty("SecretKey");
		String bucket = propertiesUtil.getProperty("bucket");
		Auth auth = Auth.create(accessKey, secretKey);
		long expires = 3600 * 24 * 7;
		StringMap putPolicy = new StringMap();
		putPolicy.put("returnBody",
				"{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"fsize\":$(fsize)}");

		String upToken = auth.uploadToken(bucket, null, expires, putPolicy);
		result.setResCode(ResultCode.SUCCESS);
		result.setObj(upToken);
		return result;
	}

	@Override
	public String upload(File file) {

		// 构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(Zone.zone2());
		// ...其他参数参考类注释
		UploadManager uploadManager = new UploadManager(cfg);
		String accessKey = propertiesUtil.getProperty("AccessKey");
		String secretKey = propertiesUtil.getProperty("SecretKey");
		String bucket = propertiesUtil.getProperty("bucket");

		Auth auth = Auth.create(accessKey, secretKey);
		String upToken = auth.uploadToken(bucket);
		try {
			Response response = uploadManager.put(file, null, upToken);
			// 解析上传成功的结果
			DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
			return putRet.key;
		} catch (QiniuException ex) {
			Response r = ex.response;
			logger.error(r.toString());
			try {
				logger.error(r.bodyString());
			} catch (QiniuException ex2) {
				logger.error(ex2.getMessage());
			}
		}
		return null;
	}

	@Override
	public String upload(InputStream fis) {
		// 构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(Zone.zone0());
		// ...其他参数参考类注释
		UploadManager uploadManager = new UploadManager(cfg);
		String accessKey = propertiesUtil.getProperty("AccessKey");
		String secretKey = propertiesUtil.getProperty("SecretKey");
		String bucket = propertiesUtil.getProperty("bucket");

		Auth auth = Auth.create(accessKey, secretKey);
		String upToken = auth.uploadToken(bucket);
		try {
			Response response = uploadManager.put(fis, null, upToken, null, "image/png");
			// 解析上传成功的结果
			DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
			return putRet.key;
		} catch (QiniuException ex) {
			Response r = ex.response;
			logger.error(r.toString());
			try {
				logger.error(r.bodyString());
			} catch (QiniuException ex2) {
				logger.error(ex2.getMessage());
			}
		}
		return null;
	}
}
